package com.hailiang.study.javase.java.util.concurrent.future;

import org.junit.Test;

import java.util.concurrent.*;

/**
 * @author hailiang.jiang
 * @date 2021年07月30 17:24
 */
public class FutureTaskTest {

    @Test
    public void testFuture() throws ExecutionException, InterruptedException {
        String fn = fn();
        System.out.println(fn);
        TimeUnit.SECONDS.sleep(10);
    }


    public String fn() throws ExecutionException, InterruptedException {
        System.out.println("=====testFuture start========");
        ExecutorService executor = Executors.newCachedThreadPool();

        Future<String> userFuture = executor.submit(new UserTask("张三"));
        Future<String> emailFuture = executor.submit(new EmailTask("李四"));


        if (userFuture.isDone()) {
            System.out.println(userFuture.get());
        }

        if (emailFuture.isDone()) {
            System.out.println(emailFuture.get());
        }

        executor.shutdown();

        System.out.println("=====testFuture end========");

        return "fn";
    }
}
